<?php

class User extends CActiveRecord {

    const STATUS_NOTACTIVE = 0;
    const STATUS_ACTIVE = 1;
    const STATUS_BANNED = 2;

    const ROLE_GUEST = 0;
    const ROLE_USER = 1;
    const ROLE_MODERATOR = 2;
    const ROLE_ADMIN = 3;

    protected static $salt = '';

    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    public function tableName() {
        return 'User';
    }

    public function rules() {
        return array(
            array('username', 'length', 'max' => 36, 'min' => 3),
            array('email', 'email'),
            array('email', 'unique'),
            array('username, email, password', 'required'),
            array('status, role, lastvisit', 'safe'),
        );
    }

    public function relations() {
        return array(
            'service' => array(self::HAS_MANY, 'Service', 'owner_id','order'=>'service.t_update DESC', 'together' => false),
            'suggest' => array(self::HAS_MANY, 'Suggest', 'owner_id','order'=>'suggest.t_update DESC', 'together' => false),
        );
    }

    public function beforeValidate() {
        if($this->isNewRecord) {
            $this->lastvisit = time();
            $this->t_create = time();
        }
        return true;
    }

    static public function encrypt($value) {
        return md5(self::$salt . $value . self::$salt);
    }

    public function isOnline() {
        return $this->lastvisit > microtime(true) - 180;
    }

    static public function getOnlineCount() {
        return User::model()->cache(60)->count("lastvisit > UNIX_TIMESTAMP() - 180");
    }

    public function getIconOnline() {
        $alt = $this->isOnline() ? "В сети" : 'Оффлайн';
        $img = $this->isOnline() ? "online" : 'offline';
        $option = array("style" => 'vertical-align:middle;');
        return CHtml::image(Y::theme() . "/img/" . $img . ".png", $alt, $option);
    }

    public function generateActivationKey($activate = false, $password = '') {
        return $activate ? self::encrypt(microtime()) : self::encrypt(microtime() . $this->password);
    }

    public function getCountHelped() {
        return $this->helped_us * 3 - $this->helped;
    }

    public function getRoleText() {
        $roles_text = array(User::ROLE_ADMIN => 'Администратор', User::ROLE_MODERATOR => 'Модератор', User::ROLE_USER => 'Пользователь',);
        return isset($roles_text[$this->role]) ? $roles_text[$this->role] : '(null)';
    }

    public function upRating($up) {
        $this->rating = $this->rating + $up;
        $this->save();
    }

    public function downRating($down) {
        $this->rating = $this->rating - $down;
        $this->save();
    }

    public function addHelped($id) {
        $user = User::model()->findByPk($id);
        $user->helped = $user->helped + 1;
        $user->save();
    }

    public function addHelpedUs($id) {
        $user = User::model()->findByPk($id);
        $user->helped_us = $user->helped_us + 1;
        $user->save();
    }
}
